﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace QuestTools
{
    class Morse
    {
        static string[] morseCodesRusLitsNums =
        {"*-", "-***", "*--", "--*", "-**", "*", "***-", "--**", "**", "*---", "-*-",
         "*-**", "--", "-*", "---", "*--*", "*-*", "***", "-", "**-", "**-*", "****",
         "-*-*", "---*", "----", "--*-", "--*--", "-*--", "-**-", "**-**", "**--", "*-*-"};

        static string[] morseCodesEnglLits = 
        { "*-", "-***", "-*-*", "-**", "*", "**-*", "--*", "****", "**", "*---", "-*-", "*-**", "--",
          "-*", "---", "*--*", "--*-", "*-*", "***", "-", "**-", "***-", "*--", "-**-", "-*--", "--**"};

        static string[] morseCodesNums = {"-----", "*----", "**---", "***--", "****-",
          "*****", "-****", "--***", "---**", "----*"};

        public static string GetInvertedCode(string input)
        {
            //Get inverted code only for single code
            string inverted = string.Empty;
            foreach (char sym in input)
            {
                switch (sym)
                {
                    case '*':
                        inverted += '-';
                        break;
                    case '-':
                        inverted += '*';
                        break;
                    default:
                        inverted += sym;
                        break;
                }
            }
            return inverted;
        }

        public static string[] Calculate(string input)
        {
            string reg = @"\S+";
            Regex regExp = new Regex(reg);
            string[] result = {string.Empty, string.Empty};

            foreach (Match match in regExp.Matches(input))
            {
                bool finded = false;
                string resRus = "? ",
                       resEng = "? ";
                string word = match.Value;
                int i = 0;

                //numbers
                foreach (string code in morseCodesNums)
                {
                    if (code.Equals(word))
                    {
                        resRus = resEng = i.ToString();
                        result[0] += resRus;
                        result[1] += resEng;
                        finded = true;
                    }

                    i++;
                }

                if (finded) continue;

                //russian alphabet
                i = 0;
                foreach (string code in morseCodesRusLitsNums)
                {
                    if (code.Equals(word))
                    {
                        resRus = Form1.rusAlphabet[i] + " ";
                        break;
                    }
                    if (i == 5)
                        i++;

                    i++;
                }

                //english alphabet
                i = 0;
                foreach (string code in morseCodesEnglLits)
                {
                    if (code.Equals(word))
                    {
                        resEng = Form1.engAlphabet[i] + " ";
                        break;
                    }
                    i++;
                }

                result[0] += resRus;
                result[1] += resEng;
            }

            return result;
        }
    }
}
